package com.intelligentpension.fingerprintidentification

import android.content.Context
import android.hardware.fingerprint.FingerprintManager
import android.os.Build
import android.os.Bundle
import android.os.CancellationSignal
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.DialogFragment
import com.hjq.toast.ToastUtils
import javax.crypto.Cipher

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/4/13
 * describe ：
 */
class FingerPrintDialogFragment : DialogFragment() {
    private var mCipher: Cipher? = null
    private var mCancellationSignal: CancellationSignal? = null
    private lateinit var mActivity: MainActivity
    private lateinit var mFingerprintManager: FingerprintManager
    private var isSelfCancel = false

    fun setCipher(ciper: Cipher) {
        mCipher = ciper
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = getContext() as MainActivity
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mFingerprintManager = requireContext().getSystemService(FingerprintManager::class.java)
        setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var inflateView = inflater.inflate(
            androidx.biometric.R.layout.fingerprint_dialog_layout,
            container,
            false
        )

        return inflateView
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onResume() {
        super.onResume()
        startListener(mCipher)
    }

    //监听认证成功或者是失败
    @RequiresApi(Build.VERSION_CODES.M)
    private fun startListener(mCipher: Cipher?) {
        mCancellationSignal = CancellationSignal()
        mFingerprintManager.authenticate(
            FingerprintManager.CryptoObject(mCipher!!),
            mCancellationSignal,
            0,
            object : FingerprintManager.AuthenticationCallback() {
                override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
                    isSelfCancel = false
                    super.onAuthenticationError(errorCode, errString)
                }

                override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence?) {
                    super.onAuthenticationHelp(helpCode, helpString)
                    ToastUtils.show("指纹无法识别,再试一次")
                }

                override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
                    super.onAuthenticationSucceeded(result)
                    ToastUtils.show("指纹认真成功")
                    this@FingerPrintDialogFragment.dismiss()
                    println(result.toString())

                }

                override fun onAuthenticationFailed() {
                    super.onAuthenticationFailed()
                    ToastUtils.show("指纹认真失败,请再试一次")
                }
            },
            null
        )
    }

    override fun onPause() {
        super.onPause()
        stopListener()
    }

    private fun stopListener() {
        if (mCancellationSignal != null) {
            mCancellationSignal!!.cancel()
            mCancellationSignal = null
            isSelfCancel = true
        }
    }

}